#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

size_t my_strlen(const char *s)
{
    size_t len = 0;


    if(NULL == s)
    {
        return 0;
    }

    while(*s != '\0')
    {
        len++;
        s++;
    }

    return len;
}

char * my_strcpy(char *dest, char *src)
{
    if(dest != NULL || src != NULL)    //入口参数检查，剔除错误
    {
        return NULL;
    }


    char *temp = dest;

    while(*src != '\0')
    {
        *temp = *src;
        src++;
        temp++;
    }

    *temp = '\0';

    return dest;
}

    char * my_strncpy(char *dest, char *src, size_t n)
    {
        if(dest == NULL || src == NULL)
        {
            return NULL;
        }

        int i;
        char *temp = dest;

        for(int i = 0; (i < n) && (*src != '\0'); i++)  //安全性考虑
        {
            *temp = *src;
            temp++;
            src++;
        }

        for(; i < n; i++)
        {
            *temp = '\0';
            temp++;
        }


        //*temp = '\0';
        return 0;
    }

    char *my_strcat(char *dest,char *src)
    {
        if(dest == NULL || src == NULL)
        {
            return NULL;
        }

        char *temp = dest;

        while(*temp != '\0')
        {
            temp++;
        }

        while(*src != '\0')
        {
            *temp = *src;
            temp++;
            src++;
        }

        *temp = '\0';

        return dest;
    }

    char *my_strncat(char *dest,char *src, size_t n)
    {
        if(dest == NULL || src == NULL)
        {
            return NULL;
        }

        char *temp = dest;

        while(*temp != '\0')
        {
            temp++;
        }

        for(int i = 0; (i < n) && (*src != '\0'); i++)
        {
            *temp = *src;
            temp++;
            src++;
        }

        *temp = '\0';
        return dest;
    }

    int  my_strcmp(char *s1,char *s2)
    {
        if(s1 == NULL && s2 != NULL)
        {
            return -1;
        }

        if(s1 != NULL && s2 == NULL)
        {
            return 1;
        }

        if(s1 == NULL && s2 == NULL)
        {
            return 0;
        }

        while((*s1 != '\0') && (*s2 != '\0'))
        {
            if(*s1 > *s2)
            {
                return 1;
            }

            if(*s1 < *s2)
            {
                return -1;
            }

            s1++;
            s2++;
        }

        if(*s1 == '\0' && *s2 == '\0')
        {
            return 0;
        }

        if(*s1 != '\0' && *s2 == '\0')
        {
            return 1;
        }

        if(*s1 == '\0' && *s2 != '\0')
        {
            return -1;
        }
    }

    int my_strncmp(char *s1, char *s2, size_t n)
    {
        if(s1 == NULL || s2 == NULL)
        {
            return -1;
        }

        for(int i = 0; (i < n) && ((s1[i] != '\0') && (s2[i] != '\0')); i++)
        {
            if(s1[i] != s2[i])
            {
                return -1;
            }
        }

        return 0;
    }

int main(int argc, char *argv[])
{
    char *ptr = "hello world";

    char src[100] = "hello world";

    char *dest = (char *)malloc(sizeof(char) * 5);
    if(NULL == dest)      //检查malloc是否分配成功
    {
        printf("malloc error!\n");
        exit(1);
    }

    //strcpy(dest, "hello");
    
    int len = my_strlen(src);
    //int len = strlen(src);    //strlen用来计算字符串的长度
    size_t len1 = strlen("hello world");  //size_t是无符号长整型,输出的时候要加ld;

    printf("len = %d\n", len);
                            //printf("len1 = %d\n", len1);
    //printf("len1 = %ld\n", strlen(src));

    //strcpy(dest, src);
    //char *temp = my_strcpy(dest, src);

    //char *temp = my_strcpy(NULL, src); //  strcpy_s(char *dest ,int len, char *src);
    //char *temp = strncpy(dest, src, 5);
    //char *temp = my_strncpy(dest, src, 5);

    // if(NULL == temp)
    // {
    //     printf("my strcpy is error!\n");
    //     exit(1);    //退出整个程序，不在往下执行
    // }
#if 0
    char *temp = my_strncat(dest, src, 5);

    printf("dest = %s\n", dest);
    printf("temp = %s\n", temp);

    printf("dest = %p\n", dest);
    printf("temp = %p\n", temp);
#endif

    //strcpy(dest,"k");
    strcpy(dest, "hello1");
    //int ret = my_strcmp(dest, src);  // "k"  VS  "hello world"
    int ret = my_strncmp(dest, src, my_strlen(dest));

    printf("ret = %d\n", ret);

    return 0;
}